
load('summary_data/figs7_8_A12_A13_A14.rda')
source('utils.R')

c2 = render_basic(c2) +
  scale_y_continuous(labels = scales::label_percent(),
                     breaks = c(seq(-1, 2, .5), seq(3, 9, 1))) +
  scale_shape_manual(name = 'Gender',
                     values = c(20, 4),
                     labels = c('F', 'M')) +
  labs(
    x = element_blank(),
    y = '% Change from 2012 Copartisan',
    color = element_blank(),
    subtitle = 'Gender (Women, Men)'
  )

c10 = render_basic(c10) +
  scale_y_continuous(labels = scales::label_percent(),
                     breaks = c(seq(-1, 2, .5), seq(3, 9, 1))) +
  scale_shape_manual(
    name = 'Education',
    values = c(20, 4),
    labels = c('Non-Col. White', 'College White')
  ) +
  labs(
    x = element_blank(),
    y = '% Change from 2012 Copartisan',
    color = element_blank(),
    subtitle = 'Education among Whites (Non-College, College)'
  )

c12 = render_basic(c12) +
  scale_y_continuous(labels = scales::label_percent(),
                     breaks = c(seq(-1, 2, .5), seq(3, 9, 1))) +
  scale_shape_manual(name = 'Ethnicity',
                     values = c(20, 4),
                     labels = c('NW', 'W')) +
  labs(
    x = element_blank(),
    y = '% Change from 2012 Copartisan',
    color = element_blank(),
    subtitle = 'Ethnicity (POC, White)'
  )

legend = c2 %+%
  subset(c2$data, variable %in% c('Clinton', 'Trump16')) +
  scale_color_manual(
    values = c('deepskyblue', 'brown1') %>% rev(),
    labels = c('Clinton (2016)', 'Trump (2016)') %>% rev(),
    breaks = c('Trump16', 'Clinton')
  ) +
  guides(shape = 'none', color = 'legend') +
  theme(legend.position = 'bottom')

legend = get_legend_35(legend)

c2_16 = c2 %+%
  subset(c2$data, variable %in% c('Clinton', 'Trump16')) +
  scale_y_continuous(labels = scales::label_percent(),
                     breaks = c(seq(-1, 3, .5), seq(3, 7, 1))) +
  coord_cartesian(ylim = c(-1, 3)) +
  guides(color = 'none', shape = guide_legend(nrow = 1)) +
  scale_shape_manual(
    name = element_blank(),
    values = c(20, 4),
    labels = c('Women', 'Men')
  ) +
  labs(subtitle = element_blank(), shape = element_blank()) +
  theme(plot.margin = margin(t = -0.5, 1, 1, 1))

c10_16 = c10 %+%
  subset(c10$data, variable %in% c('Clinton', 'Trump16')) +
  scale_y_continuous(labels = scales::label_percent(),
                     breaks = c(seq(-1, 3, .5), seq(3, 7, 1))) +
  coord_cartesian(ylim = c(-1, 3)) +
  guides(color = 'none', shape = guide_legend(nrow = 1)) +
  scale_shape_manual(
    name = element_blank(),
    values = c(20, 4),
    labels = c('Non-College White', 'College White')
  ) +
  labs(subtitle = element_blank(),
       y = element_blank(),
       shape = element_blank()) +
  theme(plot.margin = margin(t = -0.5, 1, 1, 1))

c12_16 = c12 %+%
  subset(c12$data, variable %in% c('Clinton', 'Trump16')) +
  scale_y_continuous(labels = scales::label_percent(),
                     breaks = c(seq(-1, 3, .5), seq(3, 7, 1))) +
  coord_cartesian(ylim = c(-1, 3)) +
  guides(color = 'none', shape = guide_legend(nrow = 1)) +
  scale_shape_manual(
    name = element_blank(),
    values = c(20, 4),
    labels = c('POC', 'White')
  ) +
  labs(subtitle = element_blank(),
       y = element_blank(),
       shape = element_blank()) +
  theme(plot.margin = margin(t = -0.5, 1, 1, 1))

plot = plot_grid(
  c2_16 + guides(shape = 'none'),
  c10_16 + guides(shape = 'none'),
  c12_16 + guides(shape = 'none'),
  nrow = 1
)

plot = plot_grid(plot, legend, rel_heights = c(9, .35), nrow = 2)

shapes = plot_grid(get_legend_35(c2_16),
                   get_legend_35(c10_16),
                   get_legend_35(c12_16),
                   nrow = 1)

plot = plot_grid(shapes, plot, rel_heights = c(.5, 9), nrow = 2)

ggsave(filename = 'figures/fig7.pdf', plot = plot, width = 8, height = 5, units = 'in')
